package com.zxkj.commonpatrol.adapt;

import android.media.MediaPlayer;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.FragmentManager;

import com.SuperKotlin.pictureviewer.ImagePagerActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.czy.basecommon.utils.tools.MyImageLoader;
import com.luck.picture.lib.PictureSelector;
import com.xuexiang.xui.widget.dialog.bottomsheet.BottomSheet;
import com.xuexiang.xui.widget.toast.XToast;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.model.entity.Group;
import com.zxkj.commonpatrol.model.entity.GroupMember;
import com.zxkj.commonpatrol.model.entity.itemPicEntity;
import com.zxkj.commonpatrol.utils.MediaManager;
import com.zxkj.commonpatrol.view.VioceDialog;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class FzAdapt extends BaseQuickAdapter<GroupMember, BaseViewHolder> {
    private ArrayList<String> collect;
    private FragmentManager FragManager;

    public FzAdapt(int layoutResId, @Nullable List<GroupMember> data) {
        super(layoutResId, data);
        FragManager=ActivityTool.getInstance().getLastActivity().getSupportFragmentManager();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void convert(@NotNull BaseViewHolder helper, @Nullable GroupMember data) {

        ImageView iv_Pic =helper.itemView.findViewById(R.id.iv1);
        ImageView iv_Vioce =helper.itemView.findViewById(R.id.vioce);
        ImageView iv_Video =helper.itemView.findViewById(R.id.video);

        if (data.getPicData()!=null&&data.getPicData().size()>0){
            List<itemPicEntity> images = data.getPicData();
            List<String> collects = images.stream().map(p -> p.getPicPath()).collect(Collectors.toList());
            collect = new ArrayList<>();
            collect.addAll(collects);
            MyImageLoader.load(getContext(),data.getPicData().get(0).getPicPath(), iv_Pic, R.drawable.nodatas);

        }
       iv_Pic.setOnClickListener(v->{
           if (data.getPicData()!=null&&data.getPicData().size()>0){
                com.SuperKotlin.pictureviewer.PictureConfig config = new com.SuperKotlin.pictureviewer.PictureConfig.Builder()
                        .setListData(collect)    //图片数据List<String> list
                        .setPosition(0)    //图片下标（从第position张图片开始浏览）
                        .setIsShowNumber(true)//是否显示数字下标
                        .needDownload(false)    //是否支持图片下载
                        .setPlacrHolder(R.drawable.nodatas)    //占位符图片（图片加载完成前显示的资源图片，来源drawable或者mipmap）
                        .build();
                ImagePagerActivity.startActivity(getContext(), config);
            }else{
                XToast.error(getContext(),"暂无数据").show();
            }
        });

        // new BottomSheet.BottomListSheetBuilder(getContext())
        //
        //                            .addItem("查看大圖")
        //                            .addItem("刪除")
        //                            .setIsCenter(true)
        //                            .setOnSheetItemClickListener(new BottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
        //                                @RequiresApi(api = Build.VERSION_CODES.N)
        //                                @Override
        //                                public void onClick(BottomSheet dialog, View itemView, int position, String tag) {
        //                                    dialog.dismiss();
        //                                    List<itemPicEntity> images = getData().stream().filter(p -> p.getItemPicEntity() != null).map(p -> p.getItemPicEntity()).collect(Collectors.toList());
        //                                    List<String> collects = images.stream().map(p -> p.getPicPath()).collect(Collectors.toList());
        //                                    collect=new ArrayList<>();
        //                                    collect.addAll(collects);
        //
        //                                    if (position == 0) {
        //                                        com.SuperKotlin.pictureviewer.PictureConfig config = new com.SuperKotlin.pictureviewer.PictureConfig.Builder()
        //                                                .setListData(collect)    //图片数据List<String> list
        //                                                .setPosition(helper.getLayoutPosition())    //图片下标（从第position张图片开始浏览）
        //                                                .setIsShowNumber(true)//是否显示数字下标
        //                                                .needDownload(false)    //是否支持图片下载
        //                                                .setPlacrHolder(R.drawable.nodatas)    //占位符图片（图片加载完成前显示的资源图片，来源drawable或者mipmap）
        //                                                .build();
        //                                        ImagePagerActivity.startActivity(getContext(), config);
        //                                    } else {
        //
        //                                        remove(helper.getLayoutPosition());
        //                                    }
        //                                }
        //                            })
        //                            .build()
        //                            .show();
        List<BottomSheet.BottomListSheetBuilder.BottomSheetListItemData> mItemsVioce=new ArrayList<>();

        for (int i = 0; i < data.getVioceData().size(); i++) {


            mItemsVioce.add(new BottomSheet.BottomListSheetBuilder.BottomSheetListItemData("录音"+i,""));
        }

        iv_Vioce.setOnClickListener(v->{

            if (data.getVioceData()!=null&&data.getVioceData().size()>0) {

                BottomSheet.BottomListSheetBuilder bottomListSheetBuilder = new BottomSheet.BottomListSheetBuilder(getContext());
                bottomListSheetBuilder.setmItems(mItemsVioce);
                bottomListSheetBuilder.setIsCenter(true)
                        .setOnSheetItemClickListener(new BottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                            @Override
                            public void onClick(BottomSheet dialog, View itemView, int position, String tag) {
                                VioceDialog vioceDialog = VioceDialog.getInstance();
                                vioceDialog.show(FragManager,"");

                                MediaManager.playSound(data.getVioceData().get(position).getViocePath(), new MediaPlayer.OnCompletionListener() {
                                    @Override
                                    public void onCompletion(MediaPlayer mediaPlayer) {
                                        vioceDialog.dismiss();


                                    }
                                });
                            }
                        })
                        .build()
                        .show();


            }else{
                XToast.error(getContext(),"暂无数据").show();
            }
        });

        List<BottomSheet.BottomListSheetBuilder.BottomSheetListItemData> mItemsVideo=new ArrayList<>();

        for (int i = 0; i < data.getVideoData().size(); i++) {


            mItemsVideo.add(new BottomSheet.BottomListSheetBuilder.BottomSheetListItemData("视频"+i,""));
        }
        iv_Video.setOnClickListener(v->{

            if (data.getVideoData()!=null&&data.getVideoData().size()>0) {

                BottomSheet.BottomListSheetBuilder bottomListSheetBuilder = new BottomSheet.BottomListSheetBuilder(getContext());
                bottomListSheetBuilder.setmItems(mItemsVideo);
                bottomListSheetBuilder.setIsCenter(true)
                        .setOnSheetItemClickListener(new BottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                            @Override
                            public void onClick(BottomSheet dialog, View itemView, int position, String tag) {
                                PictureSelector.create(ActivityTool.getInstance().getLastActivity()).externalPictureVideo(data.getVideoData().get(position).getVideoPath());


                            }
                        })
                        .build()
                        .show();


            }else{
                XToast.error(getContext(),"暂无数据").show();
            }
        });

    }
}
